package com.example.exceldemo.read;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.example.exceldemo.entity.UserExcel;
import com.example.exceldemo.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @Author: yanhongwei
 * @Date: 2023/07/13 16:58:40
 * @Description: TODO
 */
@Slf4j
public class DictListener extends AnalysisEventListener<UserExcel> {


    private UserMapper userMapper;

    /**
     * ExcelEasy 官方推荐使用构造方法来注入mapper，因为其不支持Spring的IOC
     * @param userMapper
     */
    public DictListener(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    /**
     * 1.一行一行读取excel内容，从第二行开始读取（不读取表头信息）
     * @param userExcel
     * @param analysisContext
     */
    @Override
    public void invoke(UserExcel userExcel, AnalysisContext analysisContext) {
        //调用方法添加到数据库
//        UserExcel dict = new UserExcel();
//        //springframework下的BeanUtils.copyProperties是左拷贝到右
//        BeanUtils.copyProperties(userExcel,dict);
        userMapper.insert(userExcel);
    }

    /**
     * 2.读取表头信息
     * @param headMap
     * @param context
     */
    @Override
    public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
    }

    /**
     * 3.读取之后执行
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("导入完成了...");
    }

}


